#include "stdafx.h"
#include "IGaussianBlur.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IGaussianBlur::FunctionNameIGaussianBlur = "Blur(Gaussian)";

IGaussianBlur::IGaussianBlur(void) 
{
	_fnApi.SetFunctionName(IGaussianBlur::FunctionNameIGaussianBlur);
	_fnApi.SetFunctionDescription("This function blurs the image using Gaussian blur");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, 
		"GaussianKernelSizeW", "Sets the smoothing kernel size (Width). Please put in a odd number and above 0. If 0 is put in for both W and H, value is calculated from Sigma.", 
		ProcParamDef::input, 0, 99 ));

	_fnApi.AddParamDef(new ProcParamDefInt(1, 
		"GaussianKernelSizeH", "Sets the smoothing kernel size (Height). Please put in a odd number and above 0. If 0 is put in for both W and H, value is calculated from Sigma.", 
		ProcParamDef::input, 0, 99 ));

	_fnApi.AddParamDef(new ProcParamDefDouble(2, "SigmaX", "Gaussian kernel standard deviation in X direction. If this and SigmaY are both set to 0, ", ProcParamDef::input, 0, 100 ));
	_fnApi.AddParamDef(new ProcParamDefDouble(3, "SigmaY", "Gaussian kernel standard deviation in Y direction. If set to 0, this will be equal to X.", ProcParamDef::input, 0, 100 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IGaussianBlur::~IGaussianBlur(void)
{

}


bool IGaussianBlur::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamInt *GKernelSizeW = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamInt *GKernelSizeH = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(1));
		ProcParamDouble *Sigma1 = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(2));
		ProcParamDouble *Sigma2 = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(3));
		if  (GKernelSizeW != '\0' && GKernelSizeH != '\0')
		{
			/*if (((int) GKernelSizeW->GetValue() == 0 && ((int) GKernelSizeH->GetValue() == 0 && (double)Sigma1->GetValue() > 0 && (double)Sigma2->GetValue()>0) )
				|| ((int) GKernelSizeW->GetValue() > 0 && ((int) GKernelSizeH->GetValue() > 0 && (int) GKernelSizeW->GetValue() % 2 == 1 && (int) GKernelSizeH->GetValue() % 2 == 1 && (double)Sigma1->GetValue() == 0 && (double)Sigma2->GetValue() == 0))
				|| ((int) GKernelSizeW->GetValue() > 0 && ((int) GKernelSizeH->GetValue() > 0 && (double)Sigma1->GetValue() > 0 && (double)Sigma2->GetValue()>0)))*/
			{
				cv::Mat output;
				cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
				cv::Size ksize = cv::Size((int)(GKernelSizeW->GetValue()), (int)(GKernelSizeH->GetValue()));
				cv::GaussianBlur(input, output, ksize, (double)(Sigma1->GetValue()), (double)(Sigma2->GetValue()));

				cv::imwrite(step->GetIntermediateImagePathFilename(), output);

				input.release();
				output.release();
				rc = true;
			}
		}
	}
	return rc;
}